package com.spit.fam;

import android.content.Context;
import android.util.Log;

import com.orhanobut.hawk.Hawk;
import com.spit.fam.WebService.RetrofitClient;
import com.spit.fam.nike.HttpsSSLHelper;

import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import okhttp3.Dispatcher;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okio.Buffer;

public class UnsafeOkHttpClient {
    public static OkHttpClient getUnsafeOkHttpClient() {
        try {
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            Dispatcher dispatcher = new Dispatcher();
            dispatcher.setMaxRequests(1);

            String token = Hawk.get(InternalStorage.OFFLINE_CACHE.USER_TOKEN);
//            Log.e("token---------", token + "");
            if (token != null){
                Interceptor authInterceptor = chain -> {
                    Request originalRequest = chain.request();
                    Request requestWithAuth = originalRequest.newBuilder()
                            .header("Authorization", token)
                            .build();
                    return chain.proceed(requestWithAuth);
                };
                builder.addInterceptor(authInterceptor);
            }

            X509TrustManager x509TrustManager = HttpsSSLHelper.createTrustCustomTrustManager(HttpsSSLHelper.getInputStreamFromString(CER_HYH));
//            X509TrustManager x509TrustManager = HttpsSSLHelper.createTrustAllTrustManager();

            OkHttpClient okHttpClient = builder
                    .connectTimeout(5, TimeUnit.SECONDS)
                    .readTimeout(5, TimeUnit.SECONDS)
                    .writeTimeout(5, TimeUnit.SECONDS).dispatcher(dispatcher)
                    .sslSocketFactory(HttpsSSLHelper.createSSLSocketFactory(x509TrustManager),x509TrustManager)
                    .hostnameVerifier(new HttpsSSLHelper.TrustAllHostnameVerifier())
                    .build();
            return okHttpClient;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static String CER_HYH = "-----BEGIN CERTIFICATE-----\n" +
            "MIIDEjCCAfqgAwIBAgIUfJ4tSGgC0WQAWhMQw09Ce97ZMw8wDQYJKoZIhvcNAQEL\n" +
            "BQAwGDEWMBQGA1UEAwwNMTkyLjE2OC4xLjIwNDAeFw0yNDEwMTAwOTIyMDRaFw0y\n" +
            "NTEwMTAwOTIyMDRaMBgxFjAUBgNVBAMMDTE5Mi4xNjguMS4yMDQwggEiMA0GCSqG\n" +
            "SIb3DQEBAQUAA4IBDwAwggEKAoIBAQCe+u46EK16HsnKxGxLAL9HtBGdDvQx6Npr\n" +
            "u/YML76PDjRh5piM4eAnmrv+9IXEPM+p9ceWLp7xtwVB+8pD/qZx2+NTt8zPrgJH\n" +
            "s6UDS5XcPm8MRav/7Pk4ObIeNkV5GWbTnDBDkU1MmO2F73OgFtc5dfvPYvHCmXLK\n" +
            "IBVuDRqVVnhvvecA9llK5F7Fr7t0EtD6W0/vVON9I799m5k0VEXA7OjkfFKH3Syc\n" +
            "pZUfP7YZf1pAqQlKhm2fGv5UwEniRjlSRtTkLBtt+YCd6pIe1/W3/RS8gONbfom1\n" +
            "BdWHC2kh0hRJ/yjwF5QGwoNVPOZcpRNXTRvaVfQqvOZ4xQYxO9XBAgMBAAGjVDBS\n" +
            "MA8GA1UdEQQIMAaHBMCoAcwwCwYDVR0PBAQDAgWgMBMGA1UdJQQMMAoGCCsGAQUF\n" +
            "BwMBMB0GA1UdDgQWBBQ+IRi7yYVIm2IBSG93ajR4vw2krjANBgkqhkiG9w0BAQsF\n" +
            "AAOCAQEAj+g0RnHrFZhmVFC1SODpIOF8Htz2IFEQS1VKiCTANCmd/jhEzVqY0xaW\n" +
            "w8/2hFGHBn3gETI//cz79LhIgRO9vfOtE3LJtt/R2G1vHnQ0H8yLDYKBKEalM6RB\n" +
            "5Iu6PNW4Y5P0qUxzDvurVy8z2/nc1voAAPdZHTCUBE/14n9aLlKOLtdbP652os9D\n" +
            "zvOJR6arrc5Wy2SjCrE6neIerXW3yJMxyLuQZDckhAlZZCpgkXJsTxt3dhQTWyWe\n" +
            "1Gh/DQt53hqBGzNpaoUDS5nG/O1M0govKF8BpgTDGp3Z9H4vGfMuoCLrkTWaLYeg\n" +
            "1lFHMGJEiCn/YH7s+deRVYmaL0T/4Q==\n" +
            "-----END CERTIFICATE-----\n";
}
